<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <!-- <div class="box">
            <span>111</span>
        </div> -->
    </div>
    </div>
    <script>
        // 虚拟dom就是一个用于描述真实dom的一个js对象
        // 操作对象或者操作数组是不是都叫操作数据啊，操作数据的成本要比操作dom的成本低啊
        var app1 = {
            type: "div",
            props: {
                class: "box",
                children: [{
                    type: "span",
                    props:{
                        children: ["11111"]
                    }
                  
                }]
            }

        }
        // var app1 = {
        //     type: "div",
        //     props: {
        //         class: "box",
        //         children: [{
        //                 type: "span",
        //                 props: {
        //                     children: ["hellow"]
        //                 }
        //             },
        //             {
        //                 type: "span",
        //                 props: {
        //                     children: ["hellow1"]
        //                 }
        //             },
        //         ]
        //     }
        // }
        // obj = {
        //     name:"zhansg"
        // }
        // obj["name"] obj.name
        function render(jsx, root) {
            let createElement = (jsx, parent) => {
                let cur = null // 用于承接创建出来的dom元素
                let {
                    type,
                    props
                } = jsx
                if (typeof (jsx) == "string") {
                    cur = document.createTextNode(jsx)
                    parent.append(cur)
                } else {
                    cur = document.createElement(type)
                    parent.append(cur)
                    console.log(Object.keys(props))
                    Object.keys(props).forEach((item, index) => {
                        if (item == "class") {
                            cur.setAttribute('class', props[item])
                        } else if (item == "children") {
                            if (Array.isArray(props[item])) {
                                props[item].map((val => {
                                    createElement(val, cur)
                                }))
                            }
                        }
                    })
                }

            }
            createElement(jsx, root)
        }
        render(app1, app) // 将虚拟dom对象和要渲染到的容器传入rener函数
    </script>
</body>

</html>